package com.haotunet.app.goldtreasure.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.adapter.RedPacketAdapter;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.RedPacketDetailModel;
import com.haotunet.app.goldtreasure.model.RedPacketInfo;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.TipPopupView;
import com.haotunet.app.goldtreasure.views.TitleView;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout;
import com.haotunet.app.pulltorefresh.pullableView.PullableListView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class RedPacketActivity extends BaseActivity {

    @InjectView(R.id.title_view)
    TitleView titleView;
    @InjectView(R.id.red_wallet_amount)
    TextView redWalletAmount;
    @InjectView(R.id.no_red_packet_layout)
    LinearLayout noRedPacketLayout;
    @InjectView(R.id.lv_detail)
    PullableListView lvDetail;
    @InjectView(R.id.id_swipe_ly)
    PullToRefreshLayout idSwipeLy;
    @InjectView(R.id.btn_use_red_packet)
    Button mSureBtn;
    private int pageNum = 20;
    private String mRule;
    private final int GET_RED_PACKET_DETAIL_SUCCESS = 1;
    private final int LOAD_MORE_RED_PACKET_DETAIL_SUCCESS = 2;
    private Handler handle = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {

                case GET_RED_PACKET_DETAIL_SUCCESS:
                    RedPacketDetailModel model = (RedPacketDetailModel) msg.obj;
                    redWalletAmount.setText(model.getRedPacketBalance());
                    mRule = model.getRedRules();
                    List<RedPacketInfo> redPacketList = model.getRedPacketList();
                    if (redPacketList.size() == 0) {
                        noRedPacketLayout.setVisibility(View.VISIBLE);
                        mSureBtn.setEnabled(false);
                        return;
                    }
                    mSureBtn.setEnabled(true);
                    redPacketAdapter = new RedPacketAdapter(RedPacketActivity.this, redPacketList);
                    lvDetail.setAdapter(redPacketAdapter);
                    break;
                case LOAD_MORE_RED_PACKET_DETAIL_SUCCESS:
                    RedPacketDetailModel moreModel = (RedPacketDetailModel) msg.obj;
                    if (moreModel.getRedPacketList().size() == 0) {
                        UIHelper.showToast(RedPacketActivity.this, getString(R.string.last_page_hint));
                    }
                    redPacketAdapter.addDatas(moreModel.getRedPacketList());
                    break;
                default:
                    break;
            }
        }

    };
    private RedPacketAdapter redPacketAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_red_packet);
        ButterKnife.inject(this);
        initView();
        getRedPacketDetail("0", "");
    }

    private void initView() {
        titleView.getTitletextView().setVisibility(View.VISIBLE);
        titleView.getTitletextView().setText(R.string.red_packet);
        titleView.getBackView().setVisibility(View.VISIBLE);
        titleView.getRightText().setVisibility(View.VISIBLE);
        titleView.getRightText().setText("使用规则");
        RefreshData(0);
    }

    private void getRedPacketDetail(String pagenum, final String action) {
        ActionUtil actionUtil = new ActionUtil(RedPacketActivity.this);
        actionUtil.getRedPacketDetail(pagenum);
        actionUtil.setOnPassBackListener(new Interface.OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()) {
                    return;
                }
                RedPacketDetailModel redPacketModel = (RedPacketDetailModel) model;
                if (redPacketModel == null) {
                    UIHelper.showToast(RedPacketActivity.this, getString(R.string.rank_fail));
                    return;
                }
                if (redPacketModel.getErrorMsg() != null) {
                    if (redPacketModel.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
                        ActivityUtils.forceQuit(RedPacketActivity.this, getString(R.string.crowding_in_tips_txt));
                    } else {
                        UIHelper.showToast(RedPacketActivity.this, redPacketModel.getErrorMsg());
                    }
                } else {
                    if (action.equals("refresh") || action.equals("loadmore")) {
                        idSwipeLy.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                    Message msg = new Message();
                    if (action.equals("loadmore")) {
                        msg.what = LOAD_MORE_RED_PACKET_DETAIL_SUCCESS;
                        pageNum = pageNum + 20;
                    } else {
                        msg.what = GET_RED_PACKET_DETAIL_SUCCESS;
                    }
                    msg.obj = redPacketModel;
                    handle.sendMessage(msg);
                }
            }
        });
    }

    private void RefreshData(final int pagenum) {
        idSwipeLy.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
                getRedPacketDetail(pagenum + "", "refresh");
                pageNum = 20;
            }

            @Override
            public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
                getRedPacketDetail(pageNum + "", "loadmore");
            }
        });
    }


    private void skipToMain() {
        Intent intent = new Intent();
        intent.setClass(RedPacketActivity.this, MainActivity.class);
        intent.putExtra("type", "invest");
        startActivity(intent);
    }

    @OnClick({R.id.btn_use_red_packet, R.id.iv_back, R.id.txt_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_use_red_packet:
                skipToMain();
                break;
            case R.id.txt_right:
                new TipPopupView(RedPacketActivity.this, getResources().getString(R.string.use_rule), mRule, R.style.MyDialog).show();
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
